package com.wxy.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wxy.school.common.R;
import com.wxy.school.dto.CategoryDto;
import com.wxy.school.dto.ShoppingCartDto;
import com.wxy.school.entity.Dish;
import com.wxy.school.entity.ShoppingCart;
import com.wxy.school.service.DishService;
import com.wxy.school.service.ShoppingCartService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王祥宇
 * @projectName school
 * @packageName com.wxy.school.controller
 * @data 2023/4/7 10:04
 * @description: TODO
 * @version: 1.0
 */
@RestController
@RequestMapping("school/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private DishService dishService;

    @GetMapping("/getShoppingCartList")
    public R<List<ShoppingCartDto>> get(Long shopId, Long userId){
        String key = "userId" + "_" + userId + "-" + "shoppingCart" + "_" + shopId;
        List<ShoppingCartDto> shoppingCartDtoList = (List<ShoppingCartDto>) redisTemplate.opsForValue().get(key);
        if (shoppingCartDtoList != null){
            return R.success(shoppingCartDtoList);
        }
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getShopId, shopId);
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lambdaQueryWrapper);
        shoppingCartDtoList = shoppingCartList.stream().map((item) -> {
            ShoppingCartDto shoppingCartDto = new ShoppingCartDto();
            Long dishId = item.getDishId();
            LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishLambdaQueryWrapper.eq(Dish::getId, dishId);
            Dish dish = dishService.getOne(dishLambdaQueryWrapper);
            BeanUtils.copyProperties(item, shoppingCartDto);
            shoppingCartDto.setDishName(dish.getDishName());
            shoppingCartDto.setDishImage(dish.getDishImage());
            shoppingCartDto.setOldPrice(dish.getOldPrice());
            return shoppingCartDto;
        }).collect(Collectors.toList());
        redisTemplate.opsForValue().set(key, shoppingCartDtoList);
        return R.success(shoppingCartDtoList);
    }

    @PostMapping("/addShoppingCart")
    public R<String> add(@RequestBody List<ShoppingCartDto> shoppingCart){
        Long userId = shoppingCart.get(0).getUserId();
        Long shopId = shoppingCart.get(0).getShopId();
        String key = "userId" + "_" + userId + "-" + "shoppingCart" + "_" + shopId;
        redisTemplate.opsForValue().set(key, shoppingCart);
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getShopId, shopId);
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lambdaQueryWrapper);
        if (!shoppingCartList.isEmpty()){
            shoppingCartService.removeBatchByIds(shoppingCartList);
        }
        for (ShoppingCartDto shoppingCartDto : shoppingCart) {
            ShoppingCart shoppingCart1 = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDto, shoppingCart1);
            shoppingCartService.save(shoppingCart1);
        }
        return R.success("添加成功");
    }

    @DeleteMapping("/delShoppingCart")
    public R<String> delete(Long shopId, Long userId){
        String key = "userId" + "_" + userId + "-" + "shoppingCart" + "_" + shopId;
        redisTemplate.delete(key);
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getShopId, shopId);
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lambdaQueryWrapper);
        shoppingCartService.removeBatchByIds(shoppingCartList);
        return R.success("");
    }

    @PutMapping("/updateRedis")
    public R<String> updateRedis(@RequestBody List<CategoryDto> categoryDtoList){
        String key = "userId" + "_" + categoryDtoList.get(0).getDishDtoList().get(0).getUserId() + "-" + "shopId" + "_" + categoryDtoList.get(0).getDishDtoList().get(0).getShopId();
        redisTemplate.opsForValue().set(key, categoryDtoList);
        return R.success("数据更新成功");
    }
}
